//
//  Verification.m
//  MUBATE
//
//  Created by sunbin on 2017/2/15.
//  Copyright © 2017年 Quanli. All rights reserved.
//

#import "Verification.h"
#import "VerificationCode.h"
#import "ResetModel.h"
#import "Reset.h"

@interface Verification ()

@end

@implementation Verification
{
    VerificationCode *container ;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    WEAKSELF;
    [weakSelf touchEven];
    
    container = (VerificationCode *)[NHUtils loadViewFormCtl:self view:[VerificationCode LoadVerificationCodeView]];
    
    // 上一控制器操作请求验证 当前控制器在程序进入就计时
    [self countDownEen];
    
    //index = 0返回按钮  1重新发送  2确认按钮
    container.nextStepHandler = ^(NSString *account ,NSInteger index){
        switch (index) {
            case 0:{
                // 0返回上一视图 输入账号
                [NHUtils presentViewController:@"EnterAccount" ctl:weakSelf animationType:WXSTransitionAnimationTypeSysPushFromLeft isSysBackAnimation:NO];

                
            }
                break;
            case 1:{
                // 1重新验证
                [weakSelf resetHandle:SenderCode handleValue:nil handleAccount:weakSelf.account accountTy:weakSelf.ty];
                }
                break;
            case 2:{
                // 2触发 确认按钮 重置密码
                [weakSelf resetHandle:SubmitCode handleValue:account handleAccount:weakSelf.account accountTy:weakSelf.ty];
            }
                break;
            default:
                break;
        }
    };
}



- (void)resetHandle:(resetHandle)handle handleValue:(NSString *)value handleAccount:(NSString *)account accountTy:(accountType)ty{
    
    WEAKSELF;
    [weakSelf.view showLoading:@""];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf.view hideHUD];
        [ResetModel resetHandle:handle handleValue:value handleAccount:account accountTy:ty success:^(NSDictionary *loginresults, NSString *loginRequestmsg) {
            // 成功
            if (handle == SenderCode) {
                [weakSelf countDownEen];
                [weakSelf.view showLoadFinish:loginRequestmsg showLoc:bottom];
            }else{
                // 跳转重置页面
                Reset *reset = [[Reset alloc] init];
                reset.account = weakSelf.account;
                reset.ty = weakSelf.ty;
                [weakSelf presentViewController:reset animated:NO completion:^{
                    [reset.view showLoadFinish:loginRequestmsg showLoc:bottom];
                }];
            }
        } failure:^(NSDictionary *loginresults, responseState resMake, NSString *loginRequestError) {
            // 失败
            [weakSelf.view showError:loginRequestError];
            if (handle == SubmitCode) {
                [weakSelf errorToClear];
            }
            
        }];

        
    });

  
}


- (void)errorToClear{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        container.enterScode.text = nil;
        [container.enterScode becomeFirstResponder];
        container.confirmBtn.enabled = NO;
    });
    
}



// 倒计时按钮

- (void)countDownEen{
    [container.resendBtn startTime:59 title:@"重新发送" waitTittle:@"s" respond:^(BOOL isPlayComple) {
        if (isPlayComple) {
            NSLog(@"isPlayComple");
            [NHUtils setCountDown:container.resendBtn setLayerCornerRadius:0 borderWidth:0.f borderColor:nil setBackgroundColor:[UIColor PlayComplecountDownBackgroundColor_Normal_Launch] forState:UIControlStateNormal];
        }else{
            [NHUtils setCountDown:container.resendBtn setLayerCornerRadius:0 borderWidth:1.f borderColor:[UIColor noPlayComplecountDownBorderColor_Normal_Launch] setBackgroundColor:[UIColor noPlayComplecountDownBackgroundColor_Normal_Launch] forState:UIControlStateNormal];

        }
    }];
}




#warning bug 父类已经添加点击手势
- (void)touchEven{
    WEAKSELF;
    weakSelf.view.backgroundColor = [UIColor blackColor];
    
    [self.view addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        [weakSelf.view endEditing:YES];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
